package com.tianluo.tianluobookstore.repository.impl;

import com.tianluo.tianluobookstore.BookPO;
import com.tianluo.tianluobookstore.BookPOExample;
import com.tianluo.tianluobookstore.OrderPO;
import com.tianluo.tianluobookstore.OrderPOExample;
import com.tianluo.tianluobookstore.mapper.OrderPOMapper;
import com.tianluo.tianluobookstore.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Repository
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderPOMapper orderPOMapper;

    @Override
    public OrderPO queryOrderByBizSeq(String bizSeq) {

        OrderPOExample example = new OrderPOExample();
        OrderPOExample.Criteria criteria = example.createCriteria();
        criteria.andBizSeqEqualTo(bizSeq);
        List<OrderPO> orderPOList = orderPOMapper.selectByExample(example);

        if (!CollectionUtils.isEmpty(orderPOList)) {
            //唯一键，所以只有一个
            return orderPOList.get(0);
        }

        return null;
    }

    @Override
    public Integer createOrder(OrderPO orderPO) {
        return orderPOMapper.insertSelective(orderPO);
    }

    @Override
    public OrderPO queryOrderByOrderId(String orderId) {
        OrderPOExample example = new OrderPOExample();
        OrderPOExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<OrderPO> orderPOList = orderPOMapper.selectByExample(example);

        if (!CollectionUtils.isEmpty(orderPOList)) {
            //唯一键，所以只有一个
            return orderPOList.get(0);
        }

        return null;
    }

    @Override
    public int cancelOrder(String orderId) {
        return orderPOMapper.cancelOrder(orderId);
    }

    @Override
    public List<OrderPO> processTimeoutOrders(Date timeoutTime, Integer offset, Integer pageSize) {
        return orderPOMapper.processTimeoutOrders(timeoutTime, offset, pageSize);
    }

    @Override
    public int updateOrderStatus(String orderId, String status, String preStatus, String paymentStatus, String prePaymentStatus) {
        return orderPOMapper.updateOrderStatus(orderId, status, preStatus, paymentStatus, prePaymentStatus);
    }

}
